<style scoped>
.layui-row {
    overflow: hidden;
}

.select-item {
    width: 34.333333%;
    float: right;
}

.layui-row:after,
.layui-row:before {
    clear: none;
}
</style>

<template>
<div class="organization">
    <div>
        <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>添加数据类别</a>
    </div>
    <div class="layui-row">
        <div class="layui-col-lg4 select-item" style="text-align: right">
            <div class="layui-input-inline" style="width: 300px">
                <input type="text" v-model="searchName" placeholder="数据类别名称" class="layui-input key">
            </div>
            <button type="button" class="layui-btn sou" @click="search">搜索</button>
        </div>
    </div>
    <table class="layui-table layui-form">

        <thead>
            <tr>
                <th>类别名称</th>
                <th>类别编号</th>
                <th>所属大类</th>

                <th>成本收入</th>
                <th>状态</th>
                <!--                <th>说明</th>-->
                <th>创建人</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr class="id9" v-for="(val, index) in datacategory" :key="val.guid">
                <td>{{val.name}}</td>
                <td>{{val.code}}</td>
                <td>{{val.typeName}}</td>
                <td>{{val.costincomeName}}</td>
                <td v-if="val.status == 1">启用</td>
                <td v-else>禁用</td>
                <!--                    <td>{{val.remark}}</td>-->
                <td>{{val.operatorName}}</td>
                <td>{{val.createdate}}</td>
                <td><a href="javascript:;" @click="edit(val.guid)">修改</a> | <a href="javascript:;" @click="del(val.guid)">删除</a></td>
            </tr>
        </tbody>
    </table>

    <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage"></Pagination>
</div>
</template>

<script>
import Pagination from '../common/Pagination.vue'
export default {
    name: 'ListDatacategory',
    components: {
        Pagination
    },
    data() {
        return {
            datacategory: [],
            currentPage: 1,
            totalPage: 0,
            searchName: ''
        }
    },
    mounted() {
        let that = this;
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer;
            that.getPageData();
        });

    },
    methods: {
        search: function () {
            this.getPageData('search');
        },
        turnPage: function (data) {
            this.currentPage = data;
            this.getPageData();
        },
        getPageData: function (type = '') {
            let that = this;
            let loader = layui.layer.load();

            let postData = {};
            if (type != 'search') {
                postData.page = that.currentPage;
            } else {
                postData.page = 1;
            }
            postData.name = that.searchName;

            $.ajax({
                url: '/produce/Datacategory!list.action',
                type: "post",
                data: postData,
                dataType: "json",
                success: function (res) {
                    if (res.info != undefined && res.info.rows != undefined) {
                        //分页数据赋值
                        // that.currentPage = res.info.indexPage;
                        that.totalPage = res.info.pageCount;

                        that.datacategory = res.info.rows;
                    } else {
                        that.alert("系统错误");
                    }
                },
                complete: function () {
                    layer.close(loader);
                }
            });
        },
        add: function () {
            this.$parent.switchPage('AddDatacategory');
        },
        edit: function (id) {
            this.$parent.gid = id;
            this.$parent.switchPage('EditDatacategory');
        },
        del: function (id) {
            let that = this;
            layer.confirm('确定要删除该数据？', function () {
                let loader = layer.load();
                $.ajax({
                    url: '/produce/Datacategory!del.action',
                    type: "post",
                    data: {
                        guid: id
                    },
                    dataType: "json",
                    success: function (res) {
                        if (res.info == "success") {
                            that.toast("删除成功");
                            that.getPageData();
                        } else if (res.info == 'noDel') {
                            that.toast("有子数据类别，不能进行修改删除！");
                        } else {
                            that.toast('系统错误！');
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });

            })
        },

    }
}
</script>
